﻿using System.Web;
using System.Web.Routing;

namespace SilenthinkCommon.Extension
{
    /// <summary>
    /// A route class to work with a specific subDomain
    /// </summary>
    public class SubDomainRoute : Route
    {
        /// <summary>
        /// The subDomain to route against
        /// </summary>
        private readonly string subDomain;

        /// <summary>
        /// Initializes a new instance of the <see cref="SubDomainRoute"/> class.
        /// </summary>
        /// <param name="subDomain">The sub domain.</param>
        /// <param name="url">The URL.</param>
        /// <param name="routeHandler">The route handler.</param>
        public SubDomainRoute(string subDomain, string url, IRouteHandler routeHandler)
            : base(url, routeHandler)
        {
            this.subDomain = subDomain.ToLower();
        }

        /// <summary>
        /// Returns information about the requested route.
        /// </summary>
        /// <param name="httpContext">An object that encapsulates information about the HTTP request.</param>
        /// <returns>
        /// An object that contains the values from the route definition.
        /// </returns>
        public override RouteData GetRouteData(HttpContextBase httpContext)
        {
            var url = httpContext.Request.Headers["HOST"];
            var index = url.IndexOf(".");

            if (index < 0)
            {
                return null;
            }

            var possibleSubDomain = url.Substring(0, index).ToLower();

            if (possibleSubDomain == subDomain)
            {
                var result = base.GetRouteData(httpContext);
                if (result != null)
                {
                    result.DataTokens["area"] = subDomain;
                }
                return result;
            }
            return null;
        }

        /// <summary>
        /// Returns information about the URL that is associated with the route.
        /// </summary>
        /// <param name="requestContext">An object that encapsulates information about the requested route.</param>
        /// <param name="values">An object that contains the parameters for a route.</param>
        /// <returns>
        /// An object that contains information about the URL that is associated with the route.
        /// </returns>
        public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
        {
            // Checks if the area to generate the route against is this same as the subdomain
            // If so we remove the area value so it won't be added to the URL as a query parameter
            if (values.ContainsKey("Area"))
            {
                if (values["Area"].ToString().ToLower() == subDomain)
                {
                    values.Remove("Area");
                    return base.GetVirtualPath(requestContext, values);
                }
            }
            return null;
        }
    }
}